//-----------------------------------------------------------------------
// <copyright file="CountryDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class CountryDAOBase
    {
        #region Common methods
        public virtual Country CreateCountryFromReader(IDataReader reader)
        {
            Country item = new Country();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CountryID"))) item.CountryID = (int)reader["CountryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryCode"))) item.CountryCode = (string)reader["CountryCode"];
				if (!reader.IsDBNull(reader.GetOrdinal("CountryName"))) item.CountryName = (string)reader["CountryName"];
				if (!reader.IsDBNull(reader.GetOrdinal("RoadWeight"))) item.RoadWeight = (decimal)reader["RoadWeight"];
				if (!reader.IsDBNull(reader.GetOrdinal("MeasUnit"))) item.MeasUnit = (string)reader["MeasUnit"];
				if (!reader.IsDBNull(reader.GetOrdinal("IsShowOnIDSCart"))) item.IsShowOnIDSCart = (bool)reader["IsShowOnIDSCart"];
				
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCountryFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCountry methods
            
        public virtual void CreateCountry(Country country)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCountriesInsert");
                
                dbCommandWrapper.AddInParameter("@CountryCode", DbType.AnsiString, country.CountryCode);
                dbCommandWrapper.AddInParameter("@CountryName", DbType.AnsiString, country.CountryName);
				dbCommandWrapper.AddInParameter("@RoadWeight", DbType.Decimal, country.RoadWeight == Nulls.NullDecimal?DBNull.Value:(object) country.RoadWeight);
				dbCommandWrapper.AddInParameter("@MeasUnit", DbType.AnsiString, country.MeasUnit);
				dbCommandWrapper.AddInParameter("@IsShowOnIDSCart", DbType.Boolean, country.IsShowOnIDSCart);
                dbCommandWrapper.AddOutParameter("@CountryID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                country.CountryID = (int)dbCommandWrapper.GetParameterValue("@CountryID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCountryException, ex);
            }
        }

        #endregion

        #region UpdateCountry methods
        
        public virtual void UpdateCountry(Country country)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCountriesUpdate");            
                
                dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, country.CountryID);
                dbCommandWrapper.AddInParameter("@CountryCode", DbType.AnsiString, country.CountryCode);
                dbCommandWrapper.AddInParameter("@CountryName", DbType.AnsiString, country.CountryName);
				dbCommandWrapper.AddInParameter("@RoadWeight", DbType.Decimal, country.RoadWeight == Nulls.NullDecimal?DBNull.Value:(object) country.RoadWeight);
				dbCommandWrapper.AddInParameter("@MeasUnit", DbType.AnsiString, country.MeasUnit);
				dbCommandWrapper.AddInParameter("@IsShowOnIDSCart", DbType.Boolean, country.IsShowOnIDSCart);
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCountryException, ex);
            }
        }
        
        #endregion

        #region DeleteCountry methods
        public virtual void DeleteCountry(int countryID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCountriesDelete");
                
                dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, countryID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCountryException, ex);
            }
        }

        #endregion

        #region GetCountry methods
        
        public virtual Country GetCountry(int countryID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCountriesGet");
                
                dbCommandWrapper.AddInParameter("@CountryID", DbType.Int32, countryID);
                
                Country country =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        country = CreateCountryFromReader(reader);
                        reader.Close();
                    }
                }
                return country;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCountryException, ex);
            }
        }
        
        #endregion

        #region GetCountries methods
        public virtual CountryCollection GetCountries(CountryColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCountriesGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                CountryCollection countryCollection = new CountryCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        Country country = CreateCountryFromReader(reader);
                        countryCollection.Add(country);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return countryCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCountriesException, ex);
            }
        }
        
        public virtual CountryCollection GetCountries(CountryColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCountries(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}